{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 文件\n",
    "- 长久保存信息的一种数据集合\n",
    "- 常用操作\n",
    "    - 打开关闭（文件一旦打开，需要关闭操作）\n",
    "    - 读写内容\n",
    "    - 查找\n",
    "# open函数\n",
    "- open函数参数：\n",
    "    - 第一个必须有，文件名和路径\n",
    "    - mode：表明文件用什么方式打开\n",
    "        - r：只读方式打开\n",
    "        - w：写方式打开，会覆盖以前内容\n",
    "        - x：创建方式打开，如果文件已经存在， 报错\n",
    "        - a：以追加的方式对文件进行写入\n",
    "        - b：以二进制方式打开\n",
    "        - t：文本方式打开\n",
    "        - +：可读写"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# seek（offset， from）\n",
    "- 移动文件的读取位置，也叫读取指针\n",
    "- from的取值范围：\n",
    "    - 0:从文件头开始偏移\n",
    "    - 1:从文件当前位置开始偏移\n",
    "    - 2：从文件末尾偏移\n",
    "- 移动的单位是字节（byte）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# seek案例\n",
    "# 打开文件后从第5个字节处开始读取\n",
    "\n",
    "with open(r'test01.txt', 'r') as f:\n",
    "    f.seek(6,0)\n",
    "    strChar = f.read()\n",
    "    print(strChar)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 持久化 -pickle\n",
    "- 序列化（持久化，落地）：把程序运行中的信息保存到磁盘上\n",
    "- 反序列化：序列化的逆过程\n",
    "- pickle：python提供的序列化模块\n",
    "- pickle.dump：序列化\n",
    "- pickle.load：反序列化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 序列化案例\n",
    "import pickle\n",
    "\n",
    "age = 28\n",
    "with open(r'test01.txt', 'wb') as f:\n",
    "    pickle.dump(age, f)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
